package com.ruoyi.common.utils;

import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.TypeReference;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.domain.model.HtmlContent;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.spring.SpringUtils;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.List;

/**
 * Html内容工具类
 *
 * @author ruoyi
 */
public class HtmlContentUtils {

    /**
     * 设置字典缓存
     *
     * @param field 参数键
     * @param value 字典数据列表
     */
    public static void setHtmlContentFieldValue(String field, Object value) {
        SpringUtils.getBean(RedisCache.class).setCacheObject(getCacheKey(field), value);
    }

    /**
     * 根据字典类型和字典标签获取字典值
     *
     * @param field 字典类型
     * @return 字典值
     */
    public static Object getHtmlContentValueByField(String field) {
        return SpringUtils.getBean(RedisCache.class).getCacheObject(getCacheKey(field));
    }

    /**
     * 删除指定HTML内容缓存
     *
     * @param field
     */
    public static void removeHtmlContentCache(String field) {
        SpringUtils.getBean(RedisCache.class).deleteObject(getCacheKey(field));
    }

    /**
     * 清空HTML页面内容缓存
     */
    public static void clearHtmlContentCache() {
        Collection<String> keys = SpringUtils.getBean(RedisCache.class).keys(CacheConstants.HTML_CONTENT_KEY + "*");
        SpringUtils.getBean(RedisCache.class).deleteObject(keys);
    }

    /**
     * 获取HTML内容
     *
     * @return
     */
    public static HtmlContent getHtmlContent() {
        RedisCache redisCache = SpringUtils.getBean(RedisCache.class);
        Collection<String> keys = redisCache.keys(CacheConstants.HTML_CONTENT_KEY + "*");
        if (CollectionUtils.isEmpty(keys)) {
            return null;
        }
        HtmlContent htmlContent = new HtmlContent();
        htmlContent.setFooterContactContent((String) getHtmlContentValueByField("footerContactContent"));
        htmlContent.setHeaderContent((String) getHtmlContentValueByField("headerContent"));
        htmlContent.setFooterLogoContent((String) getHtmlContentValueByField("footerLogoContent"));
        htmlContent.setBanner((String) getHtmlContentValueByField("banner"));
        htmlContent.setAdvantages((List<HtmlContent.Advantage>) getHtmlContentValueByField("advantages"));
        htmlContent.setSaleServices((List<HtmlContent.Advantage>) getHtmlContentValueByField("saleServices"));
        htmlContent.setIndexBanners((List<String>) getHtmlContentValueByField("indexBanners"));
        htmlContent.setHeaderIcon((String) getHtmlContentValueByField("headerIcon"));
        htmlContent.setIndexNewsContent((String) getHtmlContentValueByField("indexNewsContent"));
        htmlContent.setIndexProductContent((String) getHtmlContentValueByField("indexProductContent"));
        htmlContent.setIndexSolutionContent((String) getHtmlContentValueByField("indexSolutionContent"));
        htmlContent.setIndexVideoContent((String) getHtmlContentValueByField("indexVideoContent"));
        return htmlContent;
    }

    /**
     * 重置HTML内容并返回重置的内容
     *
     * @return
     */
    @Deprecated
    public static HtmlContent resetHtmlContent() {
        try {
            String content = new String(Files.readAllBytes(Paths.get(RuoYiConfig.getHtmlContentDataPath())));
            System.out.println(StringUtils.format("重置HTML内容,获取到数据:{}" , content));
            if (!StringUtils.isEmpty(content)) {
                HtmlContent htmlContent = JSONObject.parseObject(content, new TypeReference<HtmlContent>() {
                });
                setHtmlContentFieldValue("footerContactContent" , htmlContent.getFooterContactContent());
                setHtmlContentFieldValue("headerContent" , htmlContent.getHeaderContent());
                setHtmlContentFieldValue("footerLogoContent" , htmlContent.getFooterLogoContent());
                setHtmlContentFieldValue("banner" , htmlContent.getBanner());
                setHtmlContentFieldValue("advantages" , htmlContent.getAdvantages());
                setHtmlContentFieldValue("saleServices" , htmlContent.getSaleServices());
                setHtmlContentFieldValue("indexBanners" , htmlContent.getIndexBanners());
                setHtmlContentFieldValue("headerIcon" , htmlContent.getHeaderIcon());
                setHtmlContentFieldValue("indexNewsContent", htmlContent.getIndexNewsContent());
                setHtmlContentFieldValue("indexProductContent", htmlContent.getIndexProductContent());
                setHtmlContentFieldValue("indexSolutionContent", htmlContent.getIndexSolutionContent());
                setHtmlContentFieldValue("indexVideoContent", htmlContent.getIndexVideoContent());
                return htmlContent;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 设置cache key
     *
     * @param htmlContentField 参数键
     * @return 缓存键key
     */
    public static String getCacheKey(String htmlContentField) {
        return CacheConstants.HTML_CONTENT_KEY + htmlContentField;
    }
}
